import {
	login
} from '../../api/user.js'
export default {
	namespaced: true,
	state: {
		_id: '',
		token: '',
		img: '',
		nickName: "",
		delivery: [],
		user:{},

	},
	getters: {
		get_token: state => state.token,
		get_img: state => state.img,
		get_nickName: state => state.nickName,
		get_id: state => state._id,
		get_delivery: state => state.delivery,
		get_user: state => state.user,
	},
	mutations: {
		set_token: (state, token) => state.token = token,
		set_img: (state, img) => state.img = img,
		set_nickName: (state, nickName) => state.nickName = nickName,
		set_id: (state, _id) => state._id = _id,
		set_delivery: (state, delivery) => state.delivery = delivery,
		push_delivery: (state, delivery) => state.delivery.push(delivery),
		set_user: (state, user) => state.user = user,
	},
	actions: {
		login: ({
			commit,
			state
		}, {
			nickName,
			img,
			code,
			success
		}) => {
			//设置昵称和头像
			commit('set_nickName', nickName)
			commit('set_img', [img]);
			// 注册请求
			login({
				nickName,
				img,
				code,
				cb: (data) => {
					// 拿到data设置token
					commit('set_token', data.token)
					success(data)
				},
			})
		},
	}
}
